/*
 * Kthread Example
 *
 * Desc: a simple example to create kthread and stop
 * Usage
 *   ./test.sh
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/delay.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>

#include <linux/miscdevice.h>
#include <linux/workqueue.h>
#include <linux/kthread.h>

static struct task_struct *my_kthread;

int my_kthread_fn(void *data)
{
	volatile int count = 0;

	while (!kthread_should_stop()) {
		pr_info("ctk: count=%d\n", count++);
		ssleep(3);
	}

	return 0;
}

static int __init mydrv_init(void)
{
	pr_info("%s...\n", __func__);

	my_kthread = kthread_create(my_kthread_fn, NULL, "my_kthread");
	if (!my_kthread) {
		pr_err("%s: kthread_create failed\n", __func__);
		return -ENOMEM;
	}

	wake_up_process(my_kthread);

	return 0;
}

static void __exit mydrv_exit(void)
{
	pr_info("%s...\n", __func__);

	if (my_kthread)
		kthread_stop(my_kthread);

}

module_init(mydrv_init);
module_exit(mydrv_exit);

MODULE_AUTHOR("mydrv");
MODULE_LICENSE("GPL");
